<script setup>
import { ref } from 'vue'
import { articleGetChannelListService } from '@/api/article'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from '@/views/article/components/ChannelEdit.vue'
import { articleDeleteChannelService } from '@/api/article'
const channelList = ref([])
const loading = ref(false)
const dialog = ref() // 弹窗
const getChannelList = async () => {
  loading.value = true // 开启加载
  const res = await articleGetChannelListService()
  channelList.value = res.data.data // 赋值给channelList
  loading.value = false // 关闭加载
}
// 获取频道列表
getChannelList()
// 刷新频道列表
const refreshChannelList = () => {
  getChannelList()
}
// 添加功能
const handleAdd = () => {
  dialog.value.openDialog({})
}
// 编辑功能
const handleEdit = (row) => {
  dialog.value.openDialog(row)
}
// 删除功能
const handleDelete = async (row) => {
  await ElMessageBox.confirm('此操作将永久删除该分类, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await articleDeleteChannelService(row.id)
  ElMessage.success('删除成功')
  getChannelList() // 重新获取分页数据
}
</script>

<template>
  <page-card title="文章分类">
    <template #extra>
      <el-button type="primary" @click="handleAdd()"> 添加分类 </el-button>
    </template>
    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column label="序号" type="index" width="150"></el-table-column>
      <el-table-column label="分类名称" prop="cate_name"></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <!--
          scope.row 当前行数据
          scope.$index 行索引
           -->
          <el-button
            :icon="Edit"
            size="small"
            type="primary"
            plain
            @click="handleEdit(scope.row, scope.$index)"
          >
          </el-button>
          <el-button
            :icon="Delete"
            size="small"
            type="danger"
            plain
            @click="handleDelete(scope.row, scope.$index)"
          >
          </el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据哦~" />
      </template>
    </el-table>
    <!-- ref="dialog" 绑定一个ref -->
    <channel-edit
      ref="dialog"
      @refresh-list="refreshChannelList"
    ></channel-edit>
  </page-card>
</template>
<style scoped></style>
